package com.fled.itsmbigscreen.service.impl;

import com.fled.itsmbigscreen.mapper.*;
import com.fled.itsmbigscreen.pojo.*;
import com.fled.itsmbigscreen.service.PduService;
import com.fled.itsmbigscreen.service.RoomService;
import com.fled.itsmbigscreen.utils.TimeTool;
import com.fled.itsmbigscreen.vo.GetCabinetListVo;
import com.fled.itsmbigscreen.vo.GetElectricityByRoomIdVo;
import com.fled.itsmbigscreen.vo.RoomListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private MachineRoomMapper machineRoomMapper;
    @Autowired
    private DeviceAlarmMapper deviceAlarmMapper;
    @Autowired
    private HuadongPduMapper huadongPduMapper;
    @Autowired
    private CabinetMapper cabinetMapper;
    @Autowired
    private PduSensorMapper pduSensorMapper;
    @Autowired
    private HuadongServerMapper huadongServerMapper;
    @Autowired
    private PduService pduService;

    @Override
    public List<RoomListVo> roomList() {
        List<MachineRoom> machineRooms = machineRoomMapper.selectByExample(new MachineRoomExample());
        List<RoomListVo> list = new ArrayList<>();
        for (MachineRoom machineRoom : machineRooms) {
            RoomListVo vo = new RoomListVo();
            GetElectricityByRoomIdVo getElectricityByRoomIdVo = pduService.getElectricityByRoomId(machineRoom.getId());
            vo.setRoomId(machineRoom.getId());
            vo.setRoomName(machineRoom.getMachineRoom());
            vo.setHumidity(getElectricityByRoomIdVo.getHumidity());
            vo.setEnergy(getElectricityByRoomIdVo.getEnergy());
            vo.setTemperature(getElectricityByRoomIdVo.getTemperature());
            list.add(vo);

        }
        return list;
    }

    /**
     * 获取告警列表
     *
     * @param roomId
     * @return
     */
    @Override
    public List<DeviceAlarm> getAlertList(Integer roomId) {
        DeviceAlarmExample deviceAlarmExample = new DeviceAlarmExample();
        DeviceAlarmExample.Criteria criteria = deviceAlarmExample.createCriteria().andOperationStatusEqualTo(0);
        if (roomId != null) {
            HuadongPduExample huadongPduExample = new HuadongPduExample();
            huadongPduExample.createCriteria().andRoomIdEqualTo(roomId);
            List<HuadongPdu> huadongPdus = huadongPduMapper.selectByExample(huadongPduExample);
            List<Integer> pduIds = new ArrayList<>();
            for (HuadongPdu pdus : huadongPdus) {
                pduIds.add(pdus.getId());
            }
            criteria.andFromIdIn(pduIds);
        }
        List<DeviceAlarm> deviceAlarms = deviceAlarmMapper.selectByExample(deviceAlarmExample);
        return deviceAlarms;
    }

    /**
     * 根据roomId获取机柜信息
     *
     * @param roomId
     * @return
     */
    @Override
    public List<GetCabinetListVo> getCabinetList(Integer roomId) {

        List<GetCabinetListVo> list = new ArrayList<>();
        CabinetExample cabinetExample = new CabinetExample();
        cabinetExample.createCriteria().andRoomIdEqualTo(roomId);
        List<Cabinet> cabinets = cabinetMapper.selectByExample(cabinetExample);
        List<Integer> CabinetIds = new ArrayList<>();
        for (Cabinet cabinet : cabinets) {
            CabinetIds.add(cabinet.getId());
        }
        HuadongServerExample huadongServerExample = new HuadongServerExample();
        huadongServerExample.createCriteria().andCabinetIdIn(CabinetIds);
        List<HuadongServer> huadongServers = huadongServerMapper.selectByExample(huadongServerExample);
        Map<Integer, List<HuadongServer>> serverMap = new HashMap<>();
        for (HuadongServer huadongServer : huadongServers) {
            List<HuadongServer> huadongServersList = serverMap.get(huadongServer.getCabinetId());
            if (huadongServersList == null) {
                huadongServersList = new ArrayList<>();
                huadongServersList.add(huadongServer);
            } else {
                huadongServersList.add(huadongServer);
            }
            serverMap.put(huadongServer.getCabinetId(), huadongServersList);
        }
        HuadongPduExample huadongPduExample = new HuadongPduExample();
        huadongPduExample.createCriteria().andCabinetIdIn(CabinetIds);
//        huadongPduExample.setOrderByClause("add_time desc");
        List<HuadongPdu> pduList = huadongPduMapper.selectByExample(huadongPduExample);
        Map<Integer, List<Integer>> pduMap = new HashMap<>();
        List<Integer> pduIds = new ArrayList<>();
        for (HuadongPdu huadongPdu : pduList) {
            List<Integer> pduList1 = pduMap.get(huadongPdu.getCabinetId());
            if (pduMap.get(huadongPdu.getCabinetId()) != null) {
                pduList1.add(huadongPdu.getId());
                pduMap.put(huadongPdu.getCabinetId(), pduList1);
            } else {
                pduList1 = new ArrayList<>();
                pduList1.add(huadongPdu.getId());
                pduMap.put(huadongPdu.getCabinetId(), pduList1);
            }
            pduIds.add(huadongPdu.getId());
        }

        for (Cabinet cabinet : cabinets) {
            GetCabinetListVo vo = new GetCabinetListVo();
//            vo.setServerRemark();
            vo.setCabinet(cabinet.getCabinet());
            vo.setCabinetId(cabinet.getId());
            vo.setRemark(cabinet.getRemark());

            if (serverMap.get(cabinet.getId()) != null) {
                List<HuadongServer> servers = serverMap.get(cabinet.getId());
                String serverNames = servers.stream().map(HuadongServer::getServerName).collect(Collectors.joining(","));
                vo.setServerRemark(serverNames);
            }
            if (pduMap.get(cabinet.getId()) != null) {
                List<Integer> pduList1 = pduMap.get(cabinet.getId());
                List<PduSensor> pduSensors = pduSensorMapper.selectLast(pduList1,TimeTool.getDayBegin(),TimeTool.getDayEnd());
                double averageHumidity = pduSensors.stream().mapToDouble(PduSensor::getHumidity).average().orElse(0D);
                double averageTemperature = pduSensors.stream().mapToDouble(PduSensor::getTemperature).average().orElse(0D);
                vo.setTemperature(TimeTool.kill2(averageTemperature));
                vo.setHumidity(TimeTool.kill2(averageHumidity));
            }
            list.add(vo);

        }
        return list;
    }

    /**
     * 简易房间列表
     *
     * @return
     */
    @Override
    public List<RoomListVo> SimpleRoomList() {
        List<MachineRoom> machineRooms = machineRoomMapper.selectByExample(new MachineRoomExample());

        List<RoomListVo> list = new ArrayList<>();
        for (MachineRoom machineRoom : machineRooms) {
            RoomListVo vo = new RoomListVo();
            vo.setRoomId(machineRoom.getId());
            vo.setRoomName(machineRoom.getMachineRoom());
            list.add(vo);

        }
        return list;
    }

    /**
     * 房间详细信息
     *
     * @param roomId
     * @return
     */
    @Override
    public RoomListVo roomInfo(Integer roomId) {
        RoomListVo roomListVo = new RoomListVo();
        MachineRoom machineRoom = machineRoomMapper.selectByPrimaryKey(roomId);
        if (machineRoom==null){
            return roomListVo;
        }
        roomListVo.setRoomName(machineRoom.getMachineRoom());
        roomListVo.setRoomId(machineRoom.getId());
        roomListVo.setLocation(machineRoom.getLocation());
        CabinetExample cabinetExample = new CabinetExample();
        cabinetExample.createCriteria().andRoomIdEqualTo(roomId);
        List<Cabinet> cabinets = cabinetMapper.selectByExample(cabinetExample);
        roomListVo.setCabinetNum(cabinets.size());
        List<Integer> cabinetIds = new ArrayList<>();
        for (Cabinet cabinet : cabinets) {
            cabinetIds.add(cabinet.getId());
        }
        if (cabinetIds.size()>0){
            HuadongServerExample huadongServerExample = new HuadongServerExample();
            huadongServerExample.createCriteria().andCabinetIdIn(cabinetIds);
            List<HuadongServer> servers = huadongServerMapper.selectByExample(huadongServerExample);
            roomListVo.setServerNum(servers.size());
        }

        return roomListVo;
    }


}
